public class MyCircularQueue {
    private int[] elem;
    public int front; //头
    public int rear; //尾
    public MyCircularQueue(int k){
        elem = new int[k];
    }
    //判断是否满了
    public boolean isFull(){
        if((rear+1)%elem.length == front){
            return true;
        }
        return false;
    }
    //判断是否为空
    public boolean isEmpty() {
        if(rear == front){
            return true;
        }
        return false;
    }
    //入队
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        //先放再移
        elem[rear] = value;
        rear = (rear+1)%elem.length;
        return true;
    }
    //出队
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }
    //获取队头元素
    public int Front() {
        if (isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if (isEmpty()){
            return -1;
        }
//        return elem[rear];
        //rear的前一个才是真正的有值的尾，判断循环零点
        int index = rear == 0 ? elem.length-1 : rear-1;
        return elem[index];
    }
}
